package cn.gdpu.course.web.controller.teacher;

import cn.gdpu.course.constant.Constant;
import cn.gdpu.course.dao.CourseDao;
import cn.gdpu.course.dao.CourseStudentDao;
import cn.gdpu.course.dao.CourseTypeDao;
import cn.gdpu.course.dao.domain.Course;
import cn.gdpu.course.dao.domain.CourseStudent;
import cn.gdpu.course.dao.domain.CourseType;
import cn.gdpu.course.web.bean.PageBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by Administrator on 2018/4/3.
 */

@Controller
@RequestMapping("/teacher/score")
public class TeaScoreController {

	@Resource
	private CourseDao courseDao;

	@Resource
	private CourseTypeDao courseTypeDao;

	@Resource
	private CourseStudentDao courseStudentDao;

	@RequestMapping(method = RequestMethod.GET)
	public String index(HttpServletRequest request, ModelMap model) {
		String username = (String) request.getSession().getAttribute(Constant.user_name);
		model.addAttribute("username", username);
		return "teacher/score";
	}

	/**
	 * 返回该老师名下所有选课信息
	 * @param model
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
	public PageBean<CourseStudent> list(ModelMap model, HttpServletRequest request, CourseStudent courseStudent) {
		PageBean<CourseStudent> data = new PageBean<CourseStudent>();
//		CourseStudent courseStudent = new CourseStudent();
		String username = (String) request.getSession().getAttribute(Constant.user_name);
		courseStudent.setTeacher(username);

//		Course course = new Course();
//		course.setTeacher(username);
//		List<Course> courses = courseDao.findByTeacher(course);
//		List<CourseStudent> courseStudents = new ArrayList<CourseStudent>();
//		for (int i = 0; i < courses.size(); i++) {
//			CourseStudent courseStudent1 = new CourseStudent();
//			courseStudent1.setBid(courses.get(i).getId());
//			List<CourseStudent> courseStudents1 = courseStudentDao.find(courseStudent1);
//			courseStudents.addAll(courseStudents1);
//		}

//		if (null == courseStudent.getSno() || "".equals(courseStudent.getSno())){
//			data.setData(new ArrayList<CourseStudent>());
//			return data;
//		}
		List<CourseStudent> courseStudents = courseStudentDao.find(courseStudent);
		data.setData(courseStudents);

		return data;
	}

	@ResponseBody
	@RequestMapping(value = "/findById", method = {RequestMethod.GET, RequestMethod.POST})
	public CourseStudent findById(ModelMap model, CourseStudent courseStudent) {
		return courseStudentDao.findById(courseStudent);
	}

	/**
	 * 待修改
	 * @param model
	 * @param courseStudent
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public Boolean update(ModelMap model, CourseStudent courseStudent) {
		CourseStudent courseStudent1 = courseStudentDao.findById(courseStudent);
		courseStudent1.setScore(courseStudent.getScore());
		Course course = new Course();
		course.setId(courseStudent1.getBid());
		Course c = courseDao.findById(course);
//		System.out.println(c.toString());
		c.setRemain(c.getRemain() + 1);
//		course.setTotal(c.getTotal() + (course.getRemain() - c.getRemain()));
		courseDao.update(c);
//		courseStudent = courseStudentDao.findById(courseStudent);
		System.out.println(courseStudent1.getScore());
		return courseStudentDao.update(courseStudent1);

	}

}
